<?php
require_once ("../src/db/connectdatabase.php");

function getRow($idobj) {
	$con = new ConnectDB();
	
	$sql = "SELECT COUNT(*) FROM infonewobjectloan WHERE idobjectloan='".$idobj."'";
	return $con -> getvalueString($sql);
}

function getdatas($idobj, $start, $lenght) {
	$con = new ConnectDB();
	
	$sql = "SELECT nid, ntitle, datecreate FROM infonewobjectloan WHERE idobjectloan='".$idobj."' ORDER BY datecreate DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}

function int_to_date($int)
{
    $time  = date("d/m/Y", $int);
    return $time;
}

$idobj = 'uu-tien-vay-nha-tra-gop';

$page = 1;
if(isset($_GET['page'])) {
	$page = $_GET['page'];
}

$arrcountrows = getRow($idobj);

$sumrecord = $arrcountrows[0][0];
$numrecord = 50;
$pageNumber = intval(($sumrecord / $numrecord));
if(($sumrecord % $numrecord) != 0) {
	$pageNumber++;
}

if($pageNumber == 0) {
	$pageNumber = 1;	
}

if($page > $pageNumber) {
	$page = $pageNumber;
}

$arrdata = getdatas($idobj, ($page-1)*$numrecord, $numrecord);
?>
<style>
.listviewcontent table {
	width: 900px;
	max-width: 900px;
}

.listviewcontent table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.listviewcontent table tr:hover td {
	background: #FFC;
}

.class-pagination{
		float: right;
		clear: left;
		padding-top: 20px;
	}
	
	.class-pageLabel{
		font-size: 11px;
		font-weight: bold;
		text-transform: uppercase;
	}

	.class-pageNow{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		background-color: #C0C0C0;
		border-radius: 4px;
		box-shadow: 0 0 10px rgba(0,0,0,.1);
		cursor: default;
	}
	
	.class-pageElem{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		box-shadow: 0 0 10px rgba(0,0,0,.2);
		border-radius: 4px;
		background-color: white;
	}

	.class-pageElem:hover{
		padding: 5px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		background-color: #C0C0C0;
		cursor: pointer;
	}
	.submitform {
		background: #004A00;
		text-indent: 1px;
		width:80px;
		height:30px;
		cursor: pointer;
		font-weight: bold;
		font-size: 14px;
	}
</style>
<h3>Thông tin ưu tiên vay mua nhà trả góp</h3>
<div>
	<div style="float:left;">	
    	<div style="float:left; width: 300px;"><a href="admin.php?content=quanlykhac&amp;as=add-info-maub&amp;id=<? echo($idobj) ?>"><img src="../images/icon-add.jpg" title="Thêm" />Thêm</a>     |     <a href="admin.php?content=quanlykhac&amp;as=<? echo($idobj) ?>">Chỉnh sửa thông tin giới thiệu</a></div>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:10px;"></div>
    <div class="listviewcontent">
    	<table>
        	<tr>
            	<th class="title" width="40px">STT</th>
            	<th class="title" width="500px">Tiêu đề</th>
                <th class="title" width="85px">Ngày C.Nhật</th>
                <th class="title">Chức Năng</th>
            </tr>
            <?php
			$i = ($page-1)*$numrecord + 1;
			foreach($arrdata as $entry) {
			?>
            <tr>
            	<td align="center"><?php echo($i) ?></td>
                <td><?php echo($entry[1]) ?></td>
                <td align="center"><?php echo(int_to_date($entry[2])) ?></td>
                <td align="center"><a href="admin.php?content=quanlykhac&amp;as=edit-info-maub&amp;id=<?php echo($entry[0]) ?>"><img style="cursor:pointer;" title="Xem Chi Tiết Thông Tin" src="../images/icon-edit.gif" height="20"></a>&nbsp; | &nbsp;<img idpr="<?php echo($entry[0]) ?>" class="class-deletequestion-link" src="../images/icon-delete.gif" height="20" title="Xóa" /></td>
            </tr>
            <?php
				$i++;
			}
			?>
        </table>
    </div>
    <div style="clear:both;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div style="float:left; width: 300px;"><a href="admin.php?content=quanlykhac&amp;as=add-info-maub&amp;id=<? echo($idobj) ?>"><img src="../images/icon-add.jpg" title="Thêm" />Thêm</a></div>
    <div style="clear:both; height:15px;"></div>
</div>
<script>

	$(document).on('click', '.class-pageElem', function() {
		page = $(this).attr('page');
		
		openwindowns('admin.php?content=quanlykhac&&as=list-uu-tien-vay-nha-tra-gop&page=' + page);
	});
	
	function openwindowns(url) {
		window.location = url;
	}

	$(document).on('click', '.class-deletequestion-link', function() {
		elem = $(this);
		id = elem.attr('idpr');
		if(confirm('Bạn thực sự muốn xóa?')) {
			$.ajax({
				type: "POST",
				url: "../src/ajax/ajaxInfoNewMauB.php",
				data:{act: 'del', idpe: id},
				dataType: "json",
				success: function(response){
					if(response == true) {
						alert("Cập nhật thành công");
						elem.parent().parent().remove();
					} else {
						alert("Cập nhật thất bại");
					}
				}
			});
		}
	});
</script>